<template>
  <el-card>
    <el-table v-loading="orderDetailsTableLoading" :data="orderDetail.orderDetails"
              border highlight-current-row
    >
      <el-table-column align="center" label="模型预览" min-width="50">
        <template #default="scope">
          <el-button link type="primary" @click="openModelDialog(scope.row.fileEntity.model)">
            {{ decodeURI(scope.row.fileEntity.model.split('/').slice(-1)[0]) }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="参数信息">
        <template #default="scope">
          <div style="display: grid;grid-template-columns: 1fr;">
            <div style="display: grid;grid-template-columns: 1fr 1fr;">
              <el-text>材料： {{ scope.row.material.name || "请选择" }}</el-text>
              <el-text>颜色： {{ scope.row.material.color || "请选择" }}</el-text>
              <el-text>单价： {{ scope.row.material.price || "无" }}</el-text>
              <el-text>精度： {{ scope.row.material.materialPrecision || "无" }}</el-text>
            </div>
            <el-text>介绍： {{ scope.row.material.remarks || "无" }}</el-text>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="数量" prop="quantity" width="150"/>
    </el-table>
  </el-card>
  <preview-model v-model="modelDialog.visible" :url="modelDialog.url"/>
</template>

<script setup>
import {onActivated, onMounted, onUnmounted, reactive, ref} from "vue";
import {useOrderDetailsStore} from "@/stores/order.js";
import {getMaterialById} from "@/apis/material.js";
import PreviewModel from "@/components/preview-model.vue";

const orderDetailsStore = useOrderDetailsStore();
const props = defineProps(["id"])
const QueryFormRef = ref();

const orderDetailsTableLoading = ref(false);
const orderDetail = ref({});

const modelDialog = reactive({
  url: "",
  visible: false,
});
/**打开模型预览 */
const openModelDialog = url => {
  modelDialog.visible = true;
  modelDialog.url = url
}
/** 查询 */
const handleQuery = async () => {
  orderDetailsTableLoading.value = true;
  orderDetail.value = orderDetailsStore.getOrderDetailsById(props.id);
  for (let i in orderDetail.value.orderDetails) {
    orderDetail.value.orderDetails[i].material = {}
    let res = await getMaterialById(orderDetail.value.orderDetails[i].materialId);
    orderDetail.value.orderDetails[i].material = res.data;
  }
  orderDetailsTableLoading.value = false;
}

/** 重置查询 */
const resetQuery = () => {
  QueryFormRef.value.resetFields();
  handleQuery();
}
onMounted(() => {
  handleQuery();
})
onUnmounted(() => {
  orderDetailsStore.removeOrderDetailsById(props.id);
})

onActivated(() => {
  handleQuery();
})
</script>
<style scoped>
:deep(.pagination) {
  display: flex;
  justify-content: center;
}

.el-main {
  padding: 0 10%;
}

.el-header {
  padding: 0;
}
</style>

